package com.daiyu.sharding.controller;

import com.daiyu.sharding.common.core.JsonResult;
import com.daiyu.sharding.domain.entity.Order;
import com.daiyu.sharding.domain.entity.Product;
import com.daiyu.sharding.domain.request.ProductReq;
import com.daiyu.sharding.domain.response.OrderRsp;
import com.daiyu.sharding.domain.response.ProductRsp;
import com.daiyu.sharding.service.ProductService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：daiyu
 * @filename OrderController
 * @date 2022-09-12 16:59
 * @description：
 */
@Slf4j
@Api(tags = "商品相关接口")
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @ApiOperationSupport(author = "daiyu")
    @ApiOperation(value = "查询商品", notes = "查询商品")
    @GetMapping("/search")
    public JsonResult<List<ProductRsp>> getOrderInfo() {
        List<ProductRsp> productRsps = productService.getAll();
        JsonResult<List<ProductRsp>> jsonResult = new JsonResult<>();
        jsonResult.setData(productRsps);
        return jsonResult;
    }

    @ApiOperationSupport(author = "daiyu")
    @ApiOperation(value = "添加商品", notes = "添加商品")
    @PostMapping("/add")
    public JsonResult<Integer> addOrderInfo(@RequestBody ProductReq productReq) {
        Product product=new Product();
        BeanUtils.copyProperties(productReq,product);
        int result = productService.insert(product);
        JsonResult<Integer> jsonResult = new JsonResult<>();
        jsonResult.setData(result);
        return jsonResult;
    }
}
